/*
 * Copyright (c) 2018, ZeeSec Technologies. All rights reserved.
 * ZeeSec PROPRIETARY/CONFIDENTIAL.
 */

package springtest.httpClient;

import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.Test;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HTMLParser;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * @author ZhiQiang Chen
 * @date 2018-11-22 11:49:03
 */

public class TestJsHtml {
    public static void main(String[] args) throws Exception {
        // https://192.168.1.145:32388
        HtmlPage page = getHtmlPage("https://192.168.1.145:32388/login.php");
        String contentType = page.getContentType();
        System.out.println(page.asXml());
        // HtmlElement documentElement = page.getDocumentElement();
        // String asXml = documentElement.asXml();
        // System.out.println(page.getCustomContent());
        // HtmlHiddenInput elementById = (HtmlHiddenInput)
        // page.getElementById("rsa_key");
        // System.out.println(elementById.asXml());
        Document document = Jsoup.parse(page.asXml());
        Element elementById = document.getElementById("rsa_key");
        System.out.println(elementById.attr("value"));
    }

    private static HtmlPage getHtmlPage(String url) throws Exception {
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60);
        webClient.getOptions().setCssEnabled(false);
        // webClient.getOptions().setMaxInMemory(500);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setUseInsecureSSL(true);// 忽略ssl认证
        webClient.getOptions().setActiveXNative(false);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());// 很重要，设置支持AJAX
        // js运行时错误，是否抛出异常
        webClient.getOptions().setThrowExceptionOnScriptError(true);
        final HtmlPage page = webClient.getPage(url);
        System.err.println("查询中，请稍候");
        // TimeUnit.SECONDS.sleep(5); // web请求数据需要时间，必须让主线程休眠片刻
        webClient.waitForBackgroundJavaScript(1000);//
        // 异步JS执行需要耗时,所以这里线程要阻塞30秒,等待异步JS执行结束
        webClient.close();
        return page;
    }

    @Test
    public void aa() throws Exception {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        HtmlPage page = getHtmlPage("https://sapling.vicp.net:44333/login.php");
        HtmlElement documentElement = page.getDocumentElement();
        String htmlCode = "<html>.........</html>";
        StringWebResponse response = new StringWebResponse(page.asText(),
                new URL("https://sapling.vicp.net:44333/login.php"));
        HtmlPage pagez = HTMLParser.parseHtml(response, webClient.getCurrentWindow());
        System.out.println(pagez.asXml());
    }

}
